// 存储学生列表数据
let tableData = [];

// 当前页
let nowPage = 1;

// 每页展示条数
let pageSize = 3;

//总页数
let allPage = 1;

// 获取学生列表数据
async function getTableData() {
    let result = await axios({
        url: '/searchStudent',
        method: 'get',
    });
    result = result.data;
    tableData = result;
    renderTable(tableData);
}

// 渲染学生列表
function renderTable(data) {
    let str = '';
    data.forEach((item, index) => {
        str += `<tr>
        <td>${item.saccount}</td>
        <td>${item.sname}</td>
        <td>${item.classid}</td>
        <td>${item.chinese}</td>
        <td>${item.math}</td>
        <td>${item.english}</td>
        <td>
            <button class="btn btn-success edit" data-toggle="modal" data-target="#editMessage">编辑</button>
            <button class="btn btn-danger del">删除</button>
        </td>
        </tr>
        `
    });
    $('#table-body').html(str);
}

// 修改学生信息
function updateStudent() {
    // let tbody = $('#table-body')[0];
    let tbody = $('#table-body');
    let saccount;
    // tbody.onclick = (e) => {
    //     console.log(e.target);
    //     e.preventDefault();
    //     saccount = e.target.parentNode.parentNode.firstElementChild.textContent;
    //     let nameInput = $('#name');
    //     let classInput = $('#class');
    //     let editBtn = $('#editSubmitBtn');
    //     console.log(editBtn);
    //     editBtn.on('click', () => {
    //         let name = nameInput.val();
    //         let classid = +classInput.val();
    //         console.log(name, classid);
    //         axios({
    //             method: 'post',
    //             url: '/updateStudent',
    //             data: {
    //                 saccount,
    //                 name,
    //                 classid
    //             }
    //         }).then(resp => {
    //             alert(resp.data.msg);
    //         })
    //     })
    // }
    tbody.on('click', (e) => {
        if (e.target.className == 'btn btn-success edit') {
            saccount = e.target.parentNode.parentNode.firstElementChild.textContent;
            let nameInput = $('#name');
            let classInput = $('#class');
            let editBtn = $('#editSubmitBtn');
            editBtn.on('click', () => {
                let name = nameInput.val();
                let classid = +classInput.val();
                console.log(name, classid);
                axios({
                    method: 'post',
                    url: '/updateStudent',
                    data: {
                        saccount,
                        name,
                        classid
                    }
                }).then(resp => {
                    alert(resp.data.msg);
                })
            })
        }
    })
}

// 删除学生信息
function deleteStudent() {
    let tbodyd = $('#table-body')[0];
    let saccount;
    tbodyd.onclick = (e) => {
        e.preventDefault();
        if (e.target.className == 'btn btn-danger del') {
            saccount = e.target.parentNode.parentNode.firstElementChild.textContent;
            let isDelete = confirm('确定要删除吗');
            if (isDelete) {
                axios({
                    method: 'post',
                    url: '/deleteStudent',
                    data: {
                        saccount
                    }
                }).then(resp => {
                    alert(resp.data.msg);
                })
            }
        } else {
            return;
        }
    }
}

getTableData();
updateStudent();
deleteStudent();